/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tofc.util;

import tofc.BaseOFC;
import tofc.elements.BaseAxis;
import tofc.elements.XAxis;
import tofc.elements.XYPanel;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.List;
import tofc.elements.BasePanel;

/**
 *
 * @author zzn
 */
public class JsonTool {

    private StringBuffer sb;

    public JsonTool() {
        sb = new StringBuffer();
    }

    public String buildJson(BasePanel object) {
        String result = "{"+visit(object).toString()+"}";
        return result;
    }

    private StringBuffer visit(Object object) {
        StringBuffer result = new StringBuffer();
        boolean addComma = false;
        for (Class c = object.getClass(); c != null; c = c.getSuperclass()) {            
            Method[] methods = c.getDeclaredMethods();
            for (Method method : methods) {
                
                if (Modifier.isPublic(method.getModifiers()) && (method.getName().startsWith("get")||method.getName().startsWith("is")) && !method.getName().equals("getClass")) {
                    
                    try {
                        Object value = method.invoke(object);
                        if (value != null) {
                            if (addComma) {
                                result.append(",");
                            }
                            if(method.getName().startsWith("get")){
                                result.append("\"").append(convertName(method.getName().substring(3))).append("\": ");
                            }else if(method.getName().startsWith("is")){
                                result.append("\"").append(convertName(method.getName().substring(2))).append("\": ");
                            }                            

                            addComma = true;
                            result.append(convertValue(value));
                        }

                    } catch (IllegalAccessException e) {
                    } catch (IllegalArgumentException e) {
                    } catch (InvocationTargetException e) {
                    }


                }
            }
        }
        return result;
    }

    private String visit(String name, Object object) {
        String result = "";
        return result;
    }

    private StringBuffer convertValue(Object object) {
        StringBuffer result = new StringBuffer();
        if (object == null) {
            result.append("null");
        } else if (object instanceof BaseOFC) {
            result.append("{").append(visit(object)).append("}");
            //string(((Class) object).getName());
        } else if (object instanceof Boolean) {
            result.append(((Boolean) object) ? "true" : "false");
            //bool(((Boolean) object));
        } else if (object instanceof Number) {
            result.append(object);
            //add(doTransform(object));
        } else if (object instanceof String) {
            result.append("\"").append(convertString((String) object)).append("\"");
            //string(object);
        } else if (object instanceof Character) {
            result.append("\"").append((String) object).append("\"");
            //string(object);
        } else if (object.getClass().isArray()) {
            //array(object);
        } else if (object instanceof List) {
            result.append(convertList((List)object));
        }
        return result;

    }

    private String convertName(String name) {
        String result = "" + name.charAt(0);
        for (int i = 1; i < name.length(); i++) {
            char c = name.charAt(i);
            if (c >='A' && c <= 'Z') {
                result = result + '-' + c;
            } else {
                result = result + c;
            }
        }
        //System.out.println(result);
        if(result.equals("X-Axis")||result.equals("Y-Axis")
                ||result.equals("Y-Axis-Right")||result.equals("Radar-Axis")
                ||result.equals("X-Legend")||result.equals("Y-Legend")){
            result=result.replace("-", "_");
        }

        return result.toLowerCase();
    }

    private String convertString(String string) {
        String result="";
        for(int i=0;i<string.length();i++){
            char c = string.charAt(i);
                if (c == '"') {
                    result = result+"\\\"";
                } else if (c == '\\') {
                    result =  result+"\\\\";
                } else if (c == '\b') {
                    result =  result+"\\b";
                } else if (c == '\f') {
                    result =  result+"\\f";
                } else if (c == '\n') {
                    result =  result+"\\n";
                } else if (c == '\r') {
                    result =  result+"\\r";
                } else if (c == '\t') {
                    result =  result+"\\t";
                } else{
                    result = result+c;
                }
        }
        return result;
    }

    private String convertUnicode(char c){
        String result="\\u";
        char c1=c;
        char c2=c;
        int j;
        for(int i=0;i<4;i++){
            c1<<=4;
            c1>>=4;
            j=c2-c1;
            c1<<=4;
            c2<<=4;
            j>>=12;
            
            switch(j){
                case 10: result+='a'; break;
                case 11: result+='b'; break;
                case 12: result+='c'; break;
                case 13: result+='d'; break;
                case 14: result+='e'; break;
                case 15: result+='f'; break;
                default : result+=j;
            }
        }
        return result;
    }

    private StringBuffer convertList(List list) {
        StringBuffer result = new StringBuffer();
        result.append("[");
        boolean addComma = false;
        Iterator it = list.iterator();
        while(it.hasNext()){
            if(addComma){
                result.append(",");
            }
            result.append(convertValue((Object)it.next()));
            addComma = true;
        }
        result.append("]");
        return result;
    }
}
